package org.flashbrighton.as3bots.views.ui 
{
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.*;
	import org.flashbrighton.as3bots.models.GameModel;

	
	public class Countdown extends Sprite
	{
		private var _model:GameModel;
		private var _timeRemaining:int;
		private var _timeText:TextField;
		
		public function Countdown(model:GameModel) 
		{
			_model = model;
			init();
		}
		
		
		private function init():void
		{
			_timeRemaining = GameModel.MAX_TIME;
			_model.timer.addEventListener(TimerEvent.TIMER, showTime);
			createText();
			showTime();
		}
		
		
		private function createText():void 
		{
			_timeText = new TextField();
			_timeText.embedFonts = true;
			_timeText.defaultTextFormat = new TextFormat("Arkitech", 42, 0xFFFFFF, true);
			_timeText.autoSize = TextFieldAutoSize.CENTER;
			_timeText.selectable = false;
			addChild(_timeText);
		}
		
		
		private function showTime(e:TimerEvent=null):void 
		{			
			_timeRemaining = GameModel.MAX_TIME - _model.timer.currentCount;
			var s:String = _timeRemaining.toString();
			if (_timeRemaining < 10) s = "0" + s;
			_timeText.text = s;
		}
		
	}

}